//
//  BaseTabBarController.m
//  YiXiu
//
//  Created by ZHY on 16/6/26.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "BaseTabBarController.h"
#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "shameViewController.h"
#import "commViewController.h"
#import "HotChatViewController.h"
#import "publishViewController.h"
#import "RigthViewController.h"
#import "pureTextViewController.h"
#import "pureImageViewController.h"
#import "voiceViewController.h"
#import "publishView.h"

@interface BaseTabBarController ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer * SwipeLeft;
@property (nonatomic, strong) UISwipeGestureRecognizer * SwipeRight;
@property (nonatomic, assign) CGPoint begin;
@property (nonatomic, assign) CGPoint old;
@property (nonatomic, assign) CGPoint new;
@property (nonatomic, assign) CGPoint end;
@property (nonatomic, strong) publishView *publice;


@property(nonatomic,weak)UIButton * selectBtn;



@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self configurationChildVC];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    NSLog(@"viewWillDisappear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}


-(void)configurationChildVC{
    [self addChildVC:[[shameViewController alloc] init]];
    [self addChildVC:[[UIViewController alloc] init]];
    
    ScrollerVC * SVC = [[ScrollerVC alloc] init];
    SVC.minheadTitleWidth = 200;
    SVC.headTitleHeight = 40;
    SVC.totalHeadWidth = 250;
    SVC.isOnNavBar = YES;
    //是否可以滑动
    SVC.isScroll = NO;
    SVC.count = 3;
    SVC.headTitleArr = @[@"推荐",@"图文",@"声音"];
    commViewController * community= [[commViewController alloc] init];
    //通过scollerVC取到导航控制器
    community.superVC = SVC;
    pureImageViewController * pureImage = [[pureImageViewController alloc] init];
    pureImage.superVC = SVC;
    voiceViewController * voice= [[voiceViewController alloc] init];
    SVC.BodyVCArr = @[community,pureImage,voice];
    [self addChildVC:SVC];
}



//添加vc
- (void)addChildVC:(UIViewController *)vc{
    BaseNavigationController * comm = [[BaseNavigationController alloc] initWithRootViewController:vc];
    if([vc isKindOfClass:[ScrollerVC class]]){
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"NavBar3"] forState:UIControlStateNormal];
        button.frame = CGRectMake(KScreenWidth -20-10, (64-20)/2, 20, 20);
//        vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        [button addTarget:self action:@selector(RightButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self addChildViewController:comm];
    
}



-(void)RightButton:(UIButton *)sender
{
    RigthViewController * rightPre = [[RigthViewController alloc] init];
    
    [self presentViewController:rightPre animated:YES completion:nil];
}



//重新配置tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    for (UIView * child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    for(int i = 0;i<3;i++){
        UIButton *button = [[UIButton alloc]init];
        button.frame = CGRectMake(i*KScreenWidth/3, 0, KScreenWidth/3,49);
        
        NSString * image = [NSString stringWithFormat:@"TabBar%d",i+1];
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        
        NSString * imageSel = [NSString stringWithFormat:@"TabBar%dSel",i+1];
        [button setImage:[UIImage imageNamed:imageSel] forState:UIControlStateSelected];
        //点击按钮切换控制器
        button.tag = 100+i;
        if (i == 1) {
            [button addTarget:self action:@selector(PubBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        }else {
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (i == self.selectedIndex) {
            [self buttonAction:button];
        }
        
        
        
        [self.tabBar addSubview:button];
    }
    
    
    [super viewWillAppear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
}




//点击按钮切换控制器
-(void)buttonAction:(UIButton *)sender
{
    self.selectedIndex = sender.tag-100;
    self.selectBtn.selected = NO;
    sender.selected = YES;
    self.selectBtn = sender;
    

}

-(void)PubBtnAction:(UIButton *)sender{
    
//    [UIView animateWithDuration:1 animations:^{
//        self.publice.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
//    }];
   
    self.selectBtn.selected = NO;
    sender.selected = YES;
    self.selectBtn = sender;

    
    [self.publice showAndCompletion:^{
        [self.publice removeFromSuperview];
        
        [self.view addSubview:self.publice];
    }];
    
}


-(publishView *)publice{

    if (!_publice) {
        
        _publice = [[publishView alloc]init];
        
        __block typeof(self) blockSelf = self;
        
        [_publice setBlock:^(UIViewController *Vc) {
           
            [(UINavigationController *)blockSelf.selectedViewController pushViewController:Vc animated:YES];
        }];
        [self.view addSubview:_publice];
    }
    
    return _publice;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
